/**
 * 
 */
package com.demo2do.fms.scheduler.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.scheduler.dao.fms.FmsDao;
import com.demo2do.fms.scheduler.dao.fms.FmsDaoSupport;
import com.demo2do.fms.scheduler.entity.Branch;
import com.demo2do.fms.scheduler.service.BranchWriter;

/**
 * @author Ysy
 *
 */
@Service("branchWriter")
public class BranchWriterImpl implements BranchWriter {
	
	private static final Logger logger = LoggerFactory.getLogger(BranchWriterImpl.class);

    @Dao
    private FmsDao fmsDao;
    
    @Autowired
    private FmsDaoSupport fmsDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.scheduler.service.BranchWriter#sync(com.demo2do.fms.scheduler.entity.Branch)
     */
    public void sync(Branch eppBranch) {
        
        String eppBranchId = eppBranch.getId();
        long eppBranchVersion = eppBranch.getVersion().longValue();
        
        logger.trace("#sync() - eppBranchId = [{}], eppBranchVersion = [{}]", eppBranchId, eppBranchVersion);
        
        // 1. get fms branch by epp branch id
        Branch fmsBranch = fmsDao.getBranch(eppBranchId);
        
        if (fmsBranch == null) {		// 2 not exist, insert
        	
        	fmsDaoSupport.saveBranch(eppBranch);
        	
        } else {
        	
        	// 3.1 exist and fms_branch_version less than epp_branch_version, edit
        	long fmsBranchVersion = fmsBranch.getVersion().longValue();
        	if (fmsBranchVersion < eppBranchVersion) {
        		fmsDao.edit(eppBranch);
        	}
        	
        	// 3.2 exist and fms_branch_version not less than epp_branch_version, do nothing
        	
        }
        
    }
    
}
